package com.neusoft.elmboot.controller;

import java.util.List;

import com.neusoft.elmboot.service.ex.DataNotFoundException;
import com.neusoft.elmboot.service.ex.DeleteException;
import com.neusoft.elmboot.service.ex.UpdateException;
import com.neusoft.elmboot.util.JsonResult;
import org.apache.ibatis.annotations.Update;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.neusoft.elmboot.po.DeliveryAddress;
import com.neusoft.elmboot.service.DeliveryAddressService;

@RestController
@RequestMapping("/deliveryAddresses")
public class DeliveryAddressController {
	public static final int OK = 200;
	public static final int DATA_NOT_FOUND = 204;
	public static final int ERROR = 500;

	@Autowired
	private DeliveryAddressService deliveryAddressService;

	@GetMapping("/userId/{userId}")
	public JsonResult<List<DeliveryAddress>> listDeliveryAddressByUserId(@PathVariable String userId) throws Exception{
		JsonResult<List<DeliveryAddress>> result = new JsonResult<List<DeliveryAddress>>();
		try {
			List<DeliveryAddress> data = deliveryAddressService.listDeliveryAddressByUserId(userId);
			result = new JsonResult<List<DeliveryAddress>>(OK, data);
			result.setMessage("获取用户的收货地址列表成功");
			return result;
		}catch(DataNotFoundException e){
			result.setState(DATA_NOT_FOUND);
			result.setMessage(e.getMessage());
			return result;
		}
	}
	
	@GetMapping("/{daId}")
	public JsonResult<DeliveryAddress> getDeliveryAddressById(@PathVariable Integer daId) throws Exception{
		JsonResult<DeliveryAddress> result = new JsonResult<DeliveryAddress>();
		try {
			DeliveryAddress data = deliveryAddressService.getDeliveryAddressById(daId);
			result = new JsonResult<DeliveryAddress>(OK, data);
			result.setMessage("获取收货地址成功");
			return result;
		} catch(DataNotFoundException e){
			result.setState(DATA_NOT_FOUND);
			result.setMessage(e.getMessage());
			return result;
		}
	}

	@PostMapping
	public JsonResult<DeliveryAddress> saveDeliveryAddress(@RequestBody DeliveryAddress deliveryAddress) throws Exception{
		JsonResult<DeliveryAddress> result = new JsonResult<DeliveryAddress>();
		try {
			int data = deliveryAddressService.saveDeliveryAddress(deliveryAddress);
			result = new JsonResult<DeliveryAddress>(OK, deliveryAddress);
			result.setMessage("保存收货地址成功");
			return result;
		}catch (DataNotFoundException e) {
			result.setState(ERROR);
			result.setMessage(e.getMessage());
			return result;
		}
	}

	@PutMapping
	public JsonResult<DeliveryAddress> updateDeliveryAddress(@RequestBody DeliveryAddress deliveryAddress) throws Exception {
		JsonResult<DeliveryAddress> result = new JsonResult<DeliveryAddress>();
		try {
			int data = deliveryAddressService.updateDeliveryAddress(deliveryAddress);
			result = new JsonResult<DeliveryAddress>(OK, deliveryAddress);
			result.setMessage("更新收货地址成功");
			return result;
		}catch (UpdateException e) {
			result.setState(ERROR);
			result.setMessage(e.getMessage());
			return result;
		}
	}

	@DeleteMapping("/{daId}")
	public JsonResult<DeliveryAddress> removeDeliveryAddress(@PathVariable Integer daId) throws Exception{
		JsonResult<DeliveryAddress> result = new JsonResult<DeliveryAddress>();
		try {
			int data = deliveryAddressService.removeDeliveryAddress(daId);
			result = new JsonResult<DeliveryAddress>(OK, null);
			result.setMessage("删除收货地址成功");
			return result;
		}catch (DeleteException e) {
			result.setState(ERROR);
			result.setMessage(e.getMessage());
			return result;
		}
	}
}
